home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Tool Chest / Toolbox / Ticket to Program 1.0 / Ticket to Program.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-10  |  14.1 KB  |  643 lines  |  [TEXT/KAHL]

  1. #include "Ticket to Program.h"
  2.  
  3.  
  4. /************** Main ********************/
  5. /*  This is main section of the program */
  6. /****************************************/
  7. main()
  8. {
  9.     ToolBoxInit();
  10.     if (Sys60rLater() )
  11.     {
  12.         DialogInit();
  13.         MenuBarInit ();
  14.         SetUpDragRect ();
  15.         MainLoop ();
  16.     }
  17. }
  18.  
  19.  
  20. /****************** ToolBoxInit **************************/
  21. /*  Initialize all of the commonly used toolbox routines */
  22. /*********************************************************/
  23. ToolBoxInit()
  24. {
  25.     InitGraf(&thePort);
  26.     InitFonts();
  27.     FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
  28.     InitWindows();
  29.     InitMenus();
  30.     TEInit();
  31.     InitDialogs(NIL_POINTER);
  32.     InitCursor();
  33. }
  34.  
  35.  
  36. /****************** Sys60rLater *********************/
  37. /*  Check version of system running to determine       */
  38. /*   if we should use GetNextEvent or WaitNextEvent */
  39. /****************************************************/
  40. Sys60rLater ()
  41. {
  42.     OSErr        status;
  43.     SysEnvRec    SysEnvData;
  44.     
  45.     status = SysEnvirons (SYS_VERSION, &SysEnvData);
  46.     if (( status != noErr) || (SysEnvData.systemVersion < 0x0600) )
  47.     {
  48.         StopAlert (BAD_SYS_ALERT, NIL_POINTER);
  49.         return (FALSE);
  50.     }
  51.     else
  52.         return (TRUE);
  53. }
  54.  
  55.  
  56. /******************* DialogInit *****************************/
  57. /*   This reads in the dialog window for the add menu item  */
  58. /************************************************************/
  59. DialogInit ()
  60. {
  61.     gSettingsDialog = GetNewDialog (BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);    
  62. }
  63.  
  64.  
  65. /******************** SetUpDragRect *******************/
  66. /*   This sets up the drag routine for windows to     */
  67. /*   omit the scroll bars.                            */
  68. /******************************************************/
  69. SetUpDragRect()
  70. {
  71.     gDragRect = screenBits.bounds;
  72.     gDragRect.left += DRAG_THRESHOLD;
  73.     gDragRect.right -= DRAG_THRESHOLD;
  74.     gDragRect.bottom -= DRAG_THRESHOLD;
  75. }
  76.  
  77.  
  78. /**************** MenuBarInit ********************/
  79. /*  This routine reads in the menu bar from the  */
  80. /*   resource file and displays it.              */
  81. /*************************************************/
  82. MenuBarInit ()
  83. {
  84.     Handle    myMenuBar;
  85.     
  86.     myMenuBar = GetNewMBar(BASE_RES_ID);
  87.     SetMenuBar(myMenuBar);
  88.     gAppleMenu = GetMHandle(APPLE_MENU_ID);
  89.     AddResMenu (gAppleMenu, 'DRVR');
  90.     
  91.     gFileMenu = GetMHandle(FILE_MENU_ID);
  92.     gEditMenu = GetMHandle(EDIT_MENU_ID);
  93.     
  94.     gMenusMenu = GetMHandle(MENUS_MENU_ID);
  95.     gFontMenu = GetMenu (FONT_MENU_ID);
  96.     InsertMenu (gFontMenu, -1);
  97.     AddResMenu (gFontMenu, 'FONT');
  98.     
  99.     gAlertMenu = GetMHandle (ALERT_MENU_ID);
  100.     
  101.     gWindowMenu = GetMHandle(WINDOW_MENU_ID);
  102.     
  103.     DrawMenuBar();
  104. }
  105.  
  106.  
  107. /************** MainLoop ****************/
  108. /*   This routine loops handling events */
  109. /*   until the user selects quit.       */
  110. /****************************************/
  111. MainLoop ()
  112. {
  113.     gDone = FALSE;
  114.  
  115.     
  116.     while (gDone == FALSE)
  117.     {
  118.         HandleEvent ();
  119.     }
  120. }
  121.  
  122.  
  123. /************** HandleEvent ********************/
  124. /*  This routine handles the parsing of events */
  125. /***********************************************/
  126. HandleEvent ()
  127. {
  128.     char        theChar;
  129.     GrafPtr        oldPort;
  130.     
  131.     WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION);
  132.     
  133.     switch (gTheEvent.what)
  134.     {         
  135.         case mouseDown:
  136.              HandleMouseDown();
  137.              break;
  138.              
  139.         case updateEvt:
  140.             if ( ! IsDAWindow (gTheEvent.message) )
  141.             {
  142.                 GetPort(&oldPort);
  143.                 SetPort(gTheEvent.message);
  144.                 BeginUpdate (gTheEvent.message);
  145.                 if (GetWRefCon (FrontWindow () ) == 10)
  146.                 {
  147.                     DrawPopUp ();
  148.                 }
  149.                 EndUpdate (gTheEvent.message);
  150.                 SetPort (oldPort);
  151.             }
  152.             break;
  153.             
  154.         case keyDown:
  155.         case autoKey:
  156.              theChar = gTheEvent.message & charCodeMask;
  157.              if (( gTheEvent.modifiers & cmdKey ) != 0)
  158.                  HandleMenuChoice (MenuKey(theChar));
  159.              break;
  160.     }
  161. }
  162.  
  163.  
  164.  
  165. /******************* HandleMouseDown *********************/
  166. /*  This routine handles the parsing of mouseDown events */
  167. /*********************************************************/
  168. HandleMouseDown ()
  169. {
  170.     WindowPtr        whichWindow;
  171.     short int        thePart, i;
  172.     long int        menuChoice, theChoice;
  173.     Point            myPoint, popUpUpperLeft;
  174.     GrafPtr            oldPort;
  175.     
  176.     thePart = FindWindow (gTheEvent.where, &whichWindow);
  177.     switch (thePart)
  178.     {
  179.         case inMenuBar:
  180.              menuChoice = MenuSelect(gTheEvent.where);
  181.              HandleMenuChoice (menuChoice);
  182.              break;
  183.                      
  184.         case inSysWindow:
  185.              SystemClick (&gTheEvent, whichWindow);
  186.              break;
  187.                      
  188.         case inDrag:
  189.              DragWindow (whichWindow, gTheEvent.where, &gDragRect);
  190.              break;        
  191.  
  192.         case inContent:
  193.             SelectWindow (whichWindow);
  194.             GetPort(&oldPort);
  195.             SetPort(whichWindow);
  196.  
  197.             myPoint = gTheEvent.where;
  198.             GlobalToLocal (&myPoint);
  199.             if (PtInRect (myPoint, &gPopUpRect) )
  200.             {
  201.                 InvertRect (&gLabelRect);
  202.                 popUpUpperLeft.v = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
  203.                 popUpUpperLeft.h = gPopUpRect.left;
  204.                 LocalToGlobal (&popUpUpperLeft);
  205.                 theChoice = PopUpMenuSelect(gPopUpMenu,
  206.                         popUpUpperLeft.v, popUpUpperLeft.h, gPopUpItem);
  207.                 InvertRect (&gLabelRect);
  208.                 if (LoWord (theChoice) > 0)
  209.                 {
  210.                     gPopUpItem = LoWord(theChoice);
  211.                     DrawPopUpNumber();
  212.                     for (i=0; i<gPopUpItem; i++)
  213.                         SysBeep(20);
  214.                 }
  215.                 SetPort (oldPort);
  216.             }
  217.             break;
  218.             
  219.         case inGoAway:
  220.              if (TrackGoAway(whichWindow, gTheEvent.where)) 
  221.                  DisposeWindow(whichWindow);
  222.              break;
  223.     }
  224. }
  225.  
  226.  
  227. /*************** IsDAWindow ******************/
  228. /*  This routine checks to see if the window */
  229. /*  that is selected is a DA window or not.  */
  230. /*********************************************/
  231. IsDAWindow (whichWindow)
  232. WindowPtr    whichWindow;
  233. {
  234.     if (whichWindow == NIL_POINTER)
  235.         return (FALSE);
  236.     else
  237.         return ( ( (WindowPeek)whichWindow)->windowKind<0);
  238. }
  239.  
  240.  
  241. /************* HandleMenuChoice ************/
  242. /*  This routine handles parsing of events */
  243. /*   when the user clicks in the menu bar. */
  244. /*******************************************/
  245. HandleMenuChoice (menuChoice)
  246. long int        menuChoice;
  247. {
  248.     int        theMenu;
  249.     int        theItem;
  250.     
  251.     if (menuChoice != 0)
  252.     {
  253.         theMenu = HiWord (menuChoice);
  254.         theItem = LoWord (menuChoice);
  255.         switch (theMenu)
  256.         {
  257.             case APPLE_MENU_ID:
  258.                 HandleAppleChoice (theItem);
  259.                 break;
  260.             
  261.             case FILE_MENU_ID:
  262.                 HandleFileChoice (theItem);
  263.                 break;
  264.         
  265.             case EDIT_MENU_ID:
  266.                 HandleEditChoice (theItem);
  267.                 break;
  268.                 
  269.             case MENUS_MENU_ID:
  270.                 HandleMenusChoice (theItem);
  271.                 break;
  272.                 
  273.             case ALERT_MENU_ID:
  274.                 HandleAlertChoice (theItem);
  275.                 break;
  276.                 
  277.             case WINDOW_MENU_ID:
  278.                 HandleWindowChoice (theItem);
  279.                 break;
  280.         }
  281.         
  282.     }
  283.     HiliteMenu (0);
  284. }
  285.  
  286.  
  287.         
  288. /************ HandleAppleChoice *************/
  289. /*  This routine handles parsing of events  */
  290. /*  when the user clicks in the Apple menu. */
  291. /********************************************/
  292. HandleAppleChoice (theItem)
  293. int        theItem;
  294. {
  295.     Str255        accName;
  296.     int            accNumber;
  297.     
  298.     switch (theItem)
  299.     {
  300.         case ABOUT_ITEM_ID:
  301.             NoteAlert ( ABOUT_ALERT, NIL_POINTER);
  302.             break;
  303.         
  304.         default:
  305.             GetItem (gAppleMenu, theItem, accName);
  306.             accNumber = OpenDeskAcc (accName);
  307.             break;
  308.     }
  309. }
  310.  
  311.  
  312. /************** HandleFileChoice ************/
  313. /*  This routine handles parsing of events  */
  314. /*  when the user clicks in the File menu.  */
  315. /********************************************/
  316. HandleFileChoice (theItem)
  317. int        theItem;
  318. {
  319.     switch (theItem)
  320.     {    
  321.         case QUIT_ITEM:
  322.  
  323.             gDone = TRUE;
  324.             break;
  325.     }
  326. }
  327.  
  328.  
  329. /************ HandleEditChoice **************/
  330. /*  This routine handles parsing of events  */
  331. /*  when the user clicks in the Edit menu.  */
  332. /********************************************/
  333. HandleEditChoice (theItem)
  334. int        theItem;
  335. {
  336.     switch (theItem)
  337.     {
  338.         SysBeep(12);
  339.     }
  340. }
  341.  
  342.  
  343. /************ HandleMenusChoice *************/
  344. /*  This routine handles parsing of events  */
  345. /*  when the user clicks in the Menus menu. */
  346. /********************************************/
  347. HandleMenusChoice (theItem)
  348. int        theItem;
  349. {
  350.     int        itemType;
  351.     Rect    itemRect;
  352.     Handle    itemHandle;
  353.     
  354.     
  355.     switch (theItem)
  356.     {
  357.         case ADD_MENU_ITEM:
  358.             HandleDialog();
  359.             GetDItem (gSettingsDialog, TIME_FIELD, &itemType, 
  360.                       &itemHandle, &itemRect );
  361.             GetIText (itemHandle, &(savedSettings.timeString) );
  362.             AppendMenu (gMenusMenu, &(savedSettings.timeString) );
  363.             gCount = gCount + 1;
  364.             if ( gCount > 2)
  365.             {
  366.                 DisableItem (gMenusMenu, ADD_MENU_ITEM);
  367.                 break;
  368.             }
  369.             break;
  370.             
  371.         case CHECK_MENU_ITEM:
  372.             gCheck = !gCheck;
  373.             CheckItem (gMenusMenu, CHECK_MENU_ITEM, gCheck);
  374.             break;
  375.             
  376.         case TOGGLE_MENU_ITEM:
  377.             if ( !gToggle )
  378.             {
  379.                 DisableItem (gMenusMenu, TOGGLE_MENU_ITEM);
  380.                 SetItem (gMenusMenu, TOGGLE_MENU_ITEM, "\pToggle Off");
  381.                  EnableItem (gMenusMenu, TOGGLE_MENU_ITEM);
  382.                  gToggle = !gToggle;
  383.                 break;
  384.             }
  385.             else
  386.             {
  387.                 DisableItem (gMenusMenu, TOGGLE_MENU_ITEM);
  388.                 SetItem (gMenusMenu, TOGGLE_MENU_ITEM, "\pToggle On");
  389.                  EnableItem (gMenusMenu, TOGGLE_MENU_ITEM);
  390.                  gToggle = !gToggle;
  391.                 break;
  392.             }
  393.             
  394.         case SET_STYLE_ITEM:
  395.             if ( !gStyle )
  396.             {
  397.                 SetItemStyle (gMenusMenu, SET_STYLE_ITEM, gCurrentStyle == PLAIN);
  398.                 TextFace (gCurrentStyle);
  399.                 gStyle = !gStyle;
  400.                 break;
  401.             }
  402.             else
  403.             {
  404.                 SetItemStyle (gMenusMenu, SET_STYLE_ITEM, gCurrentStyle & bold);
  405.                 TextFace (gCurrentStyle);
  406.                 gStyle = !gStyle;
  407.                 break;
  408.             }
  409.             
  410.         case HIERARCHICAL_ITEM:
  411.             SysBeep(12);
  412.             break;
  413.             
  414.         case POP_UP_ITEM:
  415.             WindowInit ();
  416.             PopUpMenuBar ();
  417.             DrawPopUp ();
  418.             break;
  419.             
  420.     }
  421. }
  422.  
  423.  
  424. /************* HandleAlertChoice ************/
  425. /*  This routine handles parsing of events  */
  426. /*  when the user clicks in the Alert menu. */
  427. /********************************************/
  428. HandleAlertChoice (theItem)
  429. int        theItem;
  430. {
  431.     switch (theItem)
  432.     {
  433.         case STOP:
  434.             StopAlert (STOP_ALERT, NIL_POINTER);
  435.             break;
  436.             
  437.         case NOTE:
  438.             NoteAlert (NOTE_ALERT, NIL_POINTER);
  439.             break;
  440.             
  441.         case CAUTION:
  442.             CautionAlert (CAUTION_ALERT, NIL_POINTER);
  443.             break;
  444.     }
  445. }
  446.  
  447.  
  448. /************* HandleWindowChoice ************/
  449. /*  This routine handles parsing of events  */
  450. /*  when the user clicks in the Window menu. */
  451. /********************************************/
  452. HandleWindowChoice (theItem)
  453. int        theItem;
  454. {
  455.     WindowPtr    myWindow;
  456.     short        windowID;
  457.     
  458.     switch (theItem)
  459.     {
  460.         case document:
  461.              windowID = 401;
  462.              myWindow = GetNewWindow (windowID, nil, -1);
  463.              SetPort (myWindow);
  464.              ShowWindow (myWindow);
  465.              break;
  466.              
  467.         case noGrow:
  468.              windowID = 402;
  469.              myWindow = GetNewWindow (windowID, nil, -1);
  470.              SetPort (myWindow);
  471.              ShowWindow (myWindow);
  472.              break;
  473.              
  474.         case zoomDoc:
  475.              windowID = 403;
  476.              myWindow = GetNewWindow (windowID, nil, -1);
  477.              DrawGrowIcon (myWindow);
  478.              SetPort (myWindow);
  479.              ShowWindow (myWindow);
  480.              break;
  481.         
  482.         case zNoGrow:
  483.              windowID = 404;
  484.              myWindow = GetNewWindow (windowID, nil, -1);
  485.              SetPort (myWindow);
  486.              ShowWindow (myWindow);
  487.              break;
  488.         
  489.         case docProc:
  490.              windowID = 405;
  491.              myWindow = GetNewWindow (windowID, nil, -1);
  492.              SetPort (myWindow);
  493.              ShowWindow (myWindow);
  494.              break;
  495.         
  496.         
  497.     }
  498. }
  499.  
  500.  
  501. /******************** WindowInit ********************/
  502. /*  This routine reads in froma resource the window */
  503. /*  that will be used to contain the PopUp item     */
  504. /***************************************************/
  505. WindowInit()
  506. {    
  507.     long    value = 10;
  508.     
  509.     gPopUpWindow = GetNewWindow(BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
  510.     ShowWindow (gPopUpWindow);
  511.     SetWRefCon (gPopUpWindow, value);
  512.     SetPort(gPopUpWindow);
  513.     TextFont (systemFont);
  514.     TextMode (srcCopy);
  515. }
  516.  
  517.  
  518. /************* PopUpMenuBar **********/
  519. /*  This inserts the PopUp menu bar   */
  520. /*  which will be drawn in the window */
  521. /**************************************/
  522. PopUpMenuBar ()
  523. {
  524.     gPopUpMenu = GetMenu(POP_UP_MENU_ID);
  525.     InsertMenu (gPopUpMenu, -1);
  526.     HLock (gPopUpMenu);
  527.     gPopUpLabelWidth = StringWidth( (**gPopUpMenu).menuData);
  528.     HUnlock (gPopUpMenu);
  529. }
  530.  
  531.  
  532. /************* DrawPopUp **************/
  533. /*  This routine draws the item to be  */
  534. /*  PopUp'ed in the window             */
  535. /***************************************/
  536. DrawPopUp ()
  537. {
  538.     SetRect (&gPopUpRect, POPUP_LEFT, POPUP_TOP, POPUP_RIGHT, POPUP_BOTTOM);
  539.     FrameRect (&gPopUpRect);
  540.     
  541.     MoveTo (gPopUpRect.left+SHADOW_PIXELS, gPopUpRect.bottom);
  542.     LineTo (gPopUpRect.right, gPopUpRect.bottom);
  543.     LineTo (gPopUpRect.right, gPopUpRect.top+SHADOW_PIXELS);
  544.     
  545.     MoveTo (gPopUpRect.left - gPopUpLabelWidth - RIGHT_MARGIN, 
  546.                 gPopUpRect.bottom - BOTTOM_MARGIN);
  547.     HLock(gPopUpMenu);
  548.     DrawString( (**gPopUpMenu).menuData);
  549.     HUnlock (gPopUpMenu);
  550.     
  551.     gLabelRect.top = gPopUpRect.top + PIXEL_FOR_TOP_LINE;
  552.     gLabelRect.left = gPopUpRect.left - gPopUpLabelWidth - 
  553.                         LEFT_MARGIN - RIGHT_MARGIN;
  554.     gLabelRect.right = gPopUpRect.left;
  555.     gLabelRect.bottom = gPopUpRect.bottom;
  556.     
  557.     DrawPopUpNumber();
  558. }
  559.  
  560.  
  561. /************* DrawPopUpNumber *************/
  562. /*  This reads in the items from a resouce */
  563. /*  and displays them in the PopUp menu    */
  564. /*******************************************/
  565. DrawPopUpNumber ()
  566. {
  567.     Str255    menuItem;
  568.     int        itemLeftMargin;
  569.     
  570.     GetItem (gPopUpMenu, gPopUpItem, &menuItem);
  571.     itemLeftMargin = (gPopUpRect.right - gPopUpRect.left -
  572.                         StringWidth(menuItem)) /2;
  573.     MoveTo (gPopUpRect.left + itemLeftMargin,
  574.                 gPopUpRect.bottom - BOTTOM_MARGIN);
  575.     DrawString (menuItem);
  576. }
  577.  
  578.  
  579. /************** HandleDialog **************/
  580. /*  This routine display a ModalDialog    */
  581. /*  when the users clicks on the add      */
  582. /*  menu item.                            */
  583. /******************************************/
  584. HandleDialog ()
  585. {
  586.     int        itemHit, dialogDone = FALSE;
  587.     
  588.     ShowWindow (gSettingsDialog);
  589. /*    *(savedSettings.timeString) = nil;*/
  590. /*    SaveSettings ();*/
  591.     
  592.     while (dialogDone == FALSE)
  593.     {
  594.         ModalDialog (NIL_POINTER, &itemHit);
  595.         switch (itemHit)
  596.         {
  597.             case SAVE_BUTTON:
  598.                  HideWindow (gSettingsDialog);
  599.                  dialogDone = TRUE;
  600.                  break;
  601.                  
  602.             case CANCEL_BUTTON:
  603.                  HideWindow (gSettingsDialog);
  604.                  RestoreSettings ();
  605.                  dialogDone = TRUE;
  606.                  break;    
  607.         }
  608.     }
  609. }
  610.  
  611.  
  612. /************* SaveSettings ***************/
  613. /*  This routine saves the value the user */
  614. /*  enters into the ModalDialog box when  */
  615. /*  they select add menu item.            */
  616. /******************************************/
  617. SaveSettings ()
  618. {
  619.     int        itemType;
  620.     Rect    itemRect;
  621.     Handle    itemHandle;
  622.     
  623.     GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle, &itemRect );
  624.     GetIText (itemHandle, &(savedSettings.timeString) );
  625.     
  626. }
  627.  
  628.  
  629. /************ RestoreSettings **************/
  630. /*  This routine stores the value for the  */
  631. /*  get menu item entry if the user selects */
  632. /*  the cancel button.                      */
  633. /********************************************/
  634. RestoreSettings ()
  635. {
  636.     int        itemType;
  637.     Rect    itemRect;
  638.     Handle    itemHandle;
  639.     
  640.     GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle, &itemRect );
  641.     SetIText (itemHandle, savedSettings.timeString);
  642.  
  643. }